package com.ruoyi.dev.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.db.Db;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dev.mapper.DevBatchMapper;
import com.ruoyi.dev.domain.DevBatch;
import com.ruoyi.dev.service.IDevBatchService;

import javax.sql.DataSource;

/**
 * 批处理Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-02-25
 */
@Service
public class DevBatchServiceImpl implements IDevBatchService 
{
    private static final Logger log = LoggerFactory.getLogger(DevBatchServiceImpl.class);

    @Autowired
    private DevBatchMapper devBatchMapper;
    @Autowired
    private DataSource dataSource;

    /**
     * 执行批处理
     *
     * @param id 批处理主键
     * @return 批处理
     */
    public int runDevBatch(Long id){
        DevBatch devBatch = devBatchMapper.selectDevBatchById(id);
        if(devBatch == null){
            throw new ServiceException("批处理任务不存在");
        }
        Date last = devBatch.getLastTime() == null ? new Date(1L) : devBatch.getLastTime();
        String sql = devBatch.getSqls().replaceAll("\\$\\{lastTime\\}", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, last));
        String[] sqls = sql.split(";");
        try {
            Db db = Db.use(dataSource);
            db.executeBatch(sqls);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        devBatch.setLastTime(new Date());
        devBatchMapper.updateDevBatch(devBatch);
        return 1;
    }

    /**
     * 查询批处理
     * 
     * @param id 批处理主键
     * @return 批处理
     */
    @Override
    public DevBatch selectDevBatchById(Long id)
    {
        return devBatchMapper.selectDevBatchById(id);
    }

    /**
     * 查询批处理列表
     * 
     * @param devBatch 批处理
     * @return 批处理
     */
    @Override
    public List<DevBatch> selectDevBatchList(DevBatch devBatch)
    {
        return devBatchMapper.selectDevBatchList(devBatch);
    }

    /**
     * 新增批处理
     * 
     * @param devBatch 批处理
     * @return 结果
     */
    @Override
    public int insertDevBatch(DevBatch devBatch)
    {
        devBatch.setCreateTime(DateUtils.getNowDate());
        return devBatchMapper.insertDevBatch(devBatch);
    }

    /**
     * 修改批处理
     * 
     * @param devBatch 批处理
     * @return 结果
     */
    @Override
    public int updateDevBatch(DevBatch devBatch)
    {
        devBatch.setUpdateTime(DateUtils.getNowDate());
        return devBatchMapper.updateDevBatch(devBatch);
    }

    /**
     * 批量删除批处理
     * 
     * @param ids 需要删除的批处理主键
     * @return 结果
     */
    @Override
    public int deleteDevBatchByIds(Long[] ids)
    {
        return devBatchMapper.deleteDevBatchByIds(ids);
    }

    /**
     * 删除批处理信息
     * 
     * @param id 批处理主键
     * @return 结果
     */
    @Override
    public int deleteDevBatchById(Long id)
    {
        return devBatchMapper.deleteDevBatchById(id);
    }


}
